package com.highspeed.bdk.controller.physical.service;

import com.highspeed.bdk.TestMainApplication;
import com.highspeed.bdk.bean.entity.ViewPostion;
import com.highspeed.bdk.bean.entity.physical.*;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {TestMainApplication.class})
@Transactional
public class BoardServiceTester extends BaseDeviceServiceTester {
    private Board board;

    @Before
    public void init() {

        PhysicalTopology pt = createPhysicalTopology();
        physicalTopologyService.saveAndFlush(pt);
        physicalTopologyService.clear();

        MachineBox mb = createMachineBox();
        mb.setPhysicalTopology(pt);
        machineBoxService.saveAndFlush(mb);
        machineBoxService.clear();

        Board board = createBoard();
        board.setMachineBox(mb);

        for (int j = 0; j < 5; j++) {
            Cpu cpu = createCpu();
            board.getCpus().add(cpu);
        }
        this.board = boardService.save(board);
        createPhysicalLink(board.getCpus().get(0), board.getCpus().get(1));
        boardService.flush();
        boardService.clear();
    }

    @Test
    public void save() {
        Assert.assertNotNull(board);

        //验证Cpu数据条数
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("board.id", board.getId());
        Page<Cpu> cpus = cpuService.query(params, null);
        Assert.assertEquals(cpus.getContent().size(), 5);
    }
    @Test(expected = JpaSystemException.class)
    public void saveException() {
        Board entity = createBoard();
        boardService.saveAndFlush(entity);
    }

    @Test
    public void deleteLinks() {

        List<PhysicalLink> physicalLinks = physicalLinkService.all();
        List<PhysicalLink> mbLinks = physicalLinkService.findByBoardId(board.getId());
        Assert.assertEquals(mbLinks.size(), 1);

        boardService.delete(board.getId());
        List<PhysicalLink> nowBbLinks = physicalLinkService.findByBoardId(board.getId());
        Assert.assertEquals(nowBbLinks.size(), 0);

        Board tmp = boardService.findOne(board.getId());
        Assert.assertNull(tmp);
        List<PhysicalLink> nowLinks = physicalLinkService.all();
        Assert.assertEquals(physicalLinks.size() - mbLinks.size(), nowLinks.size());
    }

    @Test
    public void delete() {

        Map<String, Object> params = new HashMap<String, Object>();
        params = new HashMap<String, Object>();
        params.put("board.id", board.getId());
        Page<Cpu> linkCpus = cpuService.query(params, null);
        Assert.assertEquals(linkCpus.getContent().size(), 5);
        cpuService.delete(board.getCpus().get(0).getId());
        Page<Cpu> nowLinkCpus = cpuService.query(params, null);
        Assert.assertEquals(linkCpus.getContent().size() - nowLinkCpus.getContent().size(), 1);

        List<PhysicalLink> mbLinks = physicalLinkService.findByBoardId(board.getId());
        Assert.assertEquals(mbLinks.size(), 0);
    }

    @Test
    public void query() {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", board.getId());
        List<Board> list = boardService.queryToList(params, null);
        Assert.assertEquals(list.size(), 1);
    }
}
